<template>
	<view class="login">
		<view class="form">
			<u-form :model="form" ref="uForm">
				<u-form-item label="账号">
					<u-input v-model="form.username" trim placeholder="请输入账号"/>
				</u-form-item>
				<u-form-item label="密码">
					<u-input v-model="form.password" trim placeholder="请输入密码"/>
				</u-form-item>
				<u-button class="btn" type="primary" :ripple="true" @click="submit">注册</u-button>
			</u-form>
			<u-toast ref="uToast" />
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					username: '',
					password: ''
				}
			}
		},
		methods: {
			async submit(){
				try{
					const res = await this.$u.post('/user/register',this.form)
					this.$refs.uToast.show({
						title: '注册成功',
						url: '/pages/user/login'
					})
				}catch(err){
					if(err.statusCode === 422){
						this.$u.toast('用户名已存在');
					}
				}
				
			}
		}
	}
</script>

<style scoped>
	.form{
		margin:30% 20rpx;
	}
	.btn{
		margin-top:20rpx;
		width:80%;
	}
</style>
